Source = cc.Node.extend({
	posX:null,
	ctor:function(p){
		this._super();
		p.addChild(this, 11, TAG_SOURCE);
		this.init();		
	},
	init : function(){
		this.setCascadeOpacityEnabled(true);
		var source= new Button(this, 11, TAG_SOURCE1, "#yiqi/source.png",this.callback);
		source.setAnchorPoint(0.47,0.5);
		source.setPosition(300,300);
		source.setRotation(-148);
		var line1 = new Button(source,11,TAG_LINE,"#yiqi/line.png",this.callback);
		line1.setAnchorPoint(0.5,1);
		line1.setRotation(-32);
		line1.setPosition(cc.p(source.width*0.714,source.height*0.215));
		line1.setVisible(false);
		var line2 = new Button(this,11,TAG_LINE1,"#yiqi/line.png",this.callback);
		line2.setAnchorPoint(0.5,0);
		line2.setPosition(cc.p(637.5,252));
		line2.setVisible(false);
		var arrow1 = new Button(source,11,TAG_ARROW1,"#yiqi/arrow.png",this.callback);
		arrow1.setAnchorPoint(1,0.5);
		arrow1.setRotation(-40);
		arrow1.setPosition(cc.p(source.width*0.3,source.height*0.45));
		arrow1.setVisible(false);
		var arrow2 = new Button(source,11,TAG_ARROW2,"#yiqi/arrow.png",this.callback);
		arrow2.setAnchorPoint(0,0.5);
		arrow2.setRotation(-30);
		var rotate = cc.rotateBy(0,0,180);
		arrow2.setPosition(cc.p(source.width*0.840,source.height*0.693));
		arrow2.runAction(rotate);		
		arrow2.setVisible(false);
		
	},
	addListener:function(source){
		
		cc.eventManager.addListener(
			cc.EventListener.create({
				event: cc.EventListener.TOUCH_ONE_BY_ONE,
				onTouchBegan: this.onTouchBegan,
				onTouchMoved: this.onTouchMoved,
				onTouchEnded: this.onTouchEnded,
				onTouchCancelled: this.onTouchEnded
			}), source);
		
	},
	onTouchBegan:function(touch, event){
		//cc.log("onTouchBegan");
		var lightSource = event.getCurrentTarget();
		var pos = touch.getLocation();
		var rect = lightSource.getBoundingBoxToWorld();
		this.posX = 638-400*Math.sin(30*Math.PI/180);
		if(cc.rectContainsPoint(rect, pos)){
			lightSource.canMove = true;
		};
		return true;
	},
	onTouchMoved:function(touch, event){
		//cc.log("onTouchMoved");
		var lightSource = event.getCurrentTarget();
		if(lightSource.canMove){
			var arrow1 = ll.run.getChildByTag(TAG_SOURCE).getChildByTag(TAG_SOURCE1).getChildByTag(TAG_ARROW1);
			var arrow2 = ll.run.getChildByTag(TAG_SOURCE).getChildByTag(TAG_SOURCE1).getChildByTag(TAG_ARROW2);
			arrow1.setVisible(false);
			arrow2.setVisible(false);
			var line = ll.run.getChildByTag(TAG_SOURCE).getChildByTag(TAG_LINE1);
			var pos = touch.getLocation();
			var lightSourcePosX = pos.x;
			if(pos.x>1038)
				lightSourcePosX = 1038;
			if(pos.x<238)
				lightSourcePosX = 238;
			var lightSourcePosY = Math.sqrt(400*400-(638-lightSourcePosX)*(638-lightSourcePosX))+252;
			lightSource.setPosition(cc.p(lightSourcePosX,lightSourcePosY));
			var angle_a = (Math.acos((Math.abs(638 - lightSourcePosX))/400)-Math.acos((Math.abs(638 - this.posX))/400))*180/Math.PI;
			var angle_b = angle_a+2;
			
			if(lightSourcePosX>638)
			{
				angle_a = (Math.acos((Math.abs(638 - lightSourcePosX))/400)+Math.acos((Math.abs(638 - this.posX))/400))*180/Math.PI;
				angle_b = 182-angle_a;				
			}
			var angle_c = -angle_b+32;
			lightSource.setRotation(angle_b);			
			line.setRotation(angle_c);
			
		}
	},
	onTouchEnded:function(touch, event){
		//cc.log("onTouchEnded");
		var lightSource = event.getCurrentTarget();
		var arrow1 = ll.run.getChildByTag(TAG_SOURCE).getChildByTag(TAG_SOURCE1).getChildByTag(TAG_ARROW1);
		var arrow2 = ll.run.getChildByTag(TAG_SOURCE).getChildByTag(TAG_SOURCE1).getChildByTag(TAG_ARROW2);
		arrow1.setVisible(true);
		arrow2.setVisible(true);
		lightSource.canMove = false;
	},
	line1_action:function(){
		var line = this.getChildByTag(TAG_SOURCE1).getChildByTag(TAG_LINE);	
		line.setVisible(true);
		line.setScale(0.1,0.1);
		var scale = cc.scaleTo(0.5,1,2.8);
		var seq = cc.sequence(scale);
		line.runAction(seq);
		
	},
	
	
	line2_action1:function(){
		var line = this.getChildByTag(TAG_LINE1);
		line.setVisible(true);
		line.setScale(0,0);		
		line.setRotation(30);
		var scale = cc.scaleTo(0.5,1,2.8);
		var seq = cc.sequence(cc.delayTime(0.5),scale);
		line.runAction(seq);
	},
	callback:function(p){
		var action = gg.flow.flow.action;
		var func=cc.callFunc(function() {

			gg.flow.next();
		},this);
		switch(p.getTag()){	
		case TAG_SOURCE1:
			if(action==ACTION_DO1){				
				var seq = cc.sequence(cc.spawn(cc.moveTo(1,cc.p(638-400*Math.sin(30*Math.PI/180),252+400*Math.cos(30*Math.PI/180))),cc.rotateTo(1,2)),
						cc.callFunc(function() {	
						this.line1_action();
						this.line2_action1();	
						},this),cc.delayTime(1),func);			
			}
			p.runAction(seq);
			break;	
		}
	},
});
